﻿using MediatR;
using Traceability.APP.Application.Command.Traceablitys;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Traceablitys
{
    public class BatchDeleteTraceabilyCommandHandLer : IRequestHandler<BatchDeleteTraceabilyCommand, APIResult<int>>
    {
        private readonly IBaseRepostiy<Traceablity> _traceablity;

        public BatchDeleteTraceabilyCommandHandLer(IBaseRepostiy<Traceablity> traceablity)
        {
            _traceablity = traceablity;
        }
        public async Task<APIResult<int>> Handle(BatchDeleteTraceabilyCommand request, CancellationToken cancellationToken)
        {
            int count = 0;
            foreach (var id in request.Ids)
            {
                var entity = _traceablity.GetAll().FirstOrDefault(x => x.Id == id);
                if (entity != null)
                {
                    entity.IsDeleted = true;
                    await _traceablity.Update(entity);
                    count++;
                }
            }
            return new APIResult<int>
            {
                Code = ResultCode.Ok,
                Data = count,
                Message = $"成功删除{count}条数据"
            };
        }
    }
}
